package com.luluteam.pushlib.service.mqtt;


import android.util.Base64;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.luluteam.pushlib.ContextClient;
import com.luluteam.pushlib.constant.AppHolder;
import com.luluteam.pushlib.constant.CtxConstant;
import com.luluteam.pushlib.model.ClientCmdParams;
import com.luluteam.pushlib.model.PushMessage;
import com.luluteam.pushlib.model.PushReps;
import com.luluteam.pushlib.tools.NotificationHelper;
import com.luluteam.pushlib.tools.Repository;
import com.luluteam.pushlib.util.StringUtils;


/**
 * @author Guan
 * @date Created on 2017/11/30
 */
public class MqttHandler {
    private static final String TAG = "MqttHandler";


    private MqttHandler() {

    }

    /**
     * @param connected
     */
    public static void onNetwork(boolean connected) {

    }


    /**
     * 处理mqtt消息
     *
     * @param json
     */
    public static void onMessage(String json) {
        Log.i(TAG, "mqtt on Message: " + json);

        if (StringUtils.isEmpty(json)) {
            return;
        }

        try {
            String msg = new String(Base64.decode(json.getBytes(), Base64.DEFAULT));
            Log.i(TAG, "解码后msg==" + msg);
            JsonObject jsonObject = new JsonParser().parse(msg).getAsJsonObject();
            JsonElement element = jsonObject.get("action");
            if (element != null) {
                //接收到服务器发的命令
                int action = element.getAsInt();
                ClientCmdParams params = new Gson().fromJson(msg, ClientCmdParams.class);
                parseCmdParams(params);
                Log.e(TAG, "接收到服务器发的命令 ：" + params.toJson());
            } else {
                //接收到服务器推送的消息
                PushMessage pushMessage = new Gson().fromJson(msg, PushMessage.class);
                sendPushMessageReceive(pushMessage);
                NotificationHelper.sendNotification(AppHolder.appContext, pushMessage);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 接收到消息后，回应服务器，表示“成功接收”
     *
     * @param pushMessage
     */
    private static void sendPushMessageReceive(PushMessage pushMessage) {

        PushReps pushReps = new PushReps();
        pushReps.appId = AppHolder.APP_ID;
        pushReps.deviceId = AppHolder.deviceId;
        pushReps.messageId = pushMessage.getMessageId();
        Repository.postPushReceive(pushReps);
    }

    private static void parseCmdParams(ClientCmdParams params) {
        if (params.action == CtxConstant.ACTION_CONTEXT) {
            //采集情景信息，进行基于情景上下文的推荐
            String jsonTag = new String(Base64.decode(params.tag.getBytes(), Base64.DEFAULT));
            System.out.println("解析jsonTag后的数据========" + jsonTag);
            ContextClient.requestYJingPush(jsonTag);

        } else if (params.action == CtxConstant.ACTION_NORMAL) {
            Log.e(TAG, "普通的消息不应该走这里");
            return;
        }
    }


}
